package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbComponentinfo;
import com.ruoyi.system.service.ITbComponentinfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 测试数据基本信息Controller
 *
 * @author wf
 * @date 2021-09-07
 */
@Controller
@RequestMapping("/system/componentinfo")
public class TbComponentinfoController extends BaseController {

    private String prefix = "system/componentinfo";

    @Autowired
    private ITbComponentinfoService tbComponentinfoService;

    @RequiresPermissions("system:componentinfo:view")
    @GetMapping()
    public String componentinfo(ModelMap mmap) {
        List<String> stationNameList = tbComponentinfoService.selectStationNameList();
        List<String> reportNoList = tbComponentinfoService.selectReportNoList();
        List<String> registerNoList = tbComponentinfoService.selectRegisterNoList();
        List<String> deviceNameList = tbComponentinfoService.selectDeviceNameList();
        List<String> partNoList = tbComponentinfoService.selectPartNoList();
        List<String> partSerialNoList = tbComponentinfoService.selectPartSerialNoListList();
        List<String> modelList = tbComponentinfoService.selectModelList();
        List<String> manufacturerList = tbComponentinfoService.selectManufacturerList();
        mmap.put("stationNameList", stationNameList);
        mmap.put("reportNoList", reportNoList);
        mmap.put("registerNoList", registerNoList);
        mmap.put("deviceNameList", deviceNameList);
        mmap.put("partNoList", partNoList);
        mmap.put("partSerialNoList", partSerialNoList);
        mmap.put("modelList", modelList);
        mmap.put("manufacturerList", manufacturerList);
        return "system/componentinfo/componentinfo";
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("system:componentinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbComponentinfo tbComponentinfo) {
        Map<String, Object> params = tbComponentinfo.getParams();
        String time = params.get("beginTime").toString();
        if (StringUtils.isNotEmpty(time)) {
            String start=time.split(" - ")[0];
            if(start.length()>9){
                start=start.substring(0,10);
            }
            String end=time.split(" - ")[1];
            if(end.length()>9){
                end=end.substring(0,10);
            }
            params.put("beginTime", start);
            params.put("endTime", end);
            tbComponentinfo.setParams(params);
        }
        startPage();
        List<TbComponentinfo> list = tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @RequiresPermissions("system:componentinfo:export")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbComponentinfo tbComponentinfo) {
        List<TbComponentinfo> list = tbComponentinfoService.selectTbComponentinfoList(tbComponentinfo);
        ExcelUtil<TbComponentinfo> util = new ExcelUtil<TbComponentinfo>(TbComponentinfo.class);
        return util.exportExcel(list, "导出数据");
    }

    /**
     * 新增
     */
    @RequiresPermissions("system:componentinfo:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存
     */
    @Log(title = "新增保存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbComponentinfo tbComponentinfo) {
        return toAjax(tbComponentinfoService.insertTbComponentinfo(tbComponentinfo));
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:componentinfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        TbComponentinfo tbComponentinfo = tbComponentinfoService.selectTbComponentinfoById(id);
        mmap.put("tbComponentinfo", tbComponentinfo);
        return "system/componentinfo/edit";
    }

    /**
     * 修改保存
     */
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbComponentinfo tbComponentinfo) {
        return toAjax(tbComponentinfoService.updateTbComponentinfo(tbComponentinfo));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:componentinfo:remove")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {

        return toAjax(tbComponentinfoService.deleteTbComponentinfoByIds(ids));
    }

    /**
     * 查询测试站以及数量
     *
     * @param tbComponentinfo TbComponentinfo
     * @return 集合
     */
    @PostMapping("registerNoAndCountList")
    @ResponseBody
    public JSONArray selectRegisterNoAndCount(TbComponentinfo tbComponentinfo) {
        return (JSONArray) JSON.toJSON(tbComponentinfoService.selectRegisterNoAndCount(tbComponentinfo));
    }
}
